﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;
using System.Data;
using System.Globalization;
using Model;
using BLL;
using System.IO;

public partial class VehicleUI_Base_VehicleImport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Out_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            if (FileUpload1.PostedFile.ContentLength < 10485760)
            {
                try
                {
                    string name = this.FileUpload1.FileName;
                    //获取上传文件 
                    string type = name.Substring(name.LastIndexOf(".") + 1);
                    //获取上传文件的后缀 

                    string newName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "." + type;
                    //更改上传文件名 
                    string path1 = Server.MapPath("~/File/") + newName;
                    FileUpload1.SaveAs(path1);

                    LoadExcel(Server.MapPath("~/File/") + newName);

                }
                catch (Exception ex)
                {
                    UtilityService.Alert(this.Page, "导入失败!,详细:" + ex.Message);
                }
            }
            else
            {
                UtilityService.Alert(this.Page, "文件过大,请分批导入!");
            }
        }
    }


    private void LoadExcel(string path)
    {
        try
        {
            string _scName = Session["OrganID"].ToString();
            DataTable dt = null;
            try
            {
               dt = ExcelService.ImportDataTableFromExcel(path, 0, 0);
               if (File.Exists(path))
               {
                   File.Delete(path);
               }
            }
            catch
            { }
            if (dt != null)
            {
                VehicleBLL iBLL = new VehicleBLL();
                IList<Vehicle> iList = new List<Vehicle>();
                //IList<Vehicle> iListERROR = new List<Vehicle>();
                IList<int> iListErrorRowNumber = new List<int>();
                //IList<Vehicle> iListFail = new List<Vehicle>();
                IList<int> iListFailRowNumber = new List<int>();

                int count = 0;
                int row = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    //if (row == 0)
                    //{
                    //    row++;
                    //    continue;
                    //}

                    string _VehicleID = dr["车牌号"].ToString();
                    string _FrameNO = dr["车架号"].ToString();
                    string _VehicleType = dr["车架类型"].ToString();
                    string _VehiclePrice = dr["车价"].ToString();
                    string _PlanScrapDate =  dr["计划报废日期"].ToString();
                    string _StartUseDate = dr["启用日期"].ToString();
                    string _ObjectGroup = dr["所属小组"].ToString(); ;
                    string _KeepPerson = dr["接车人"].ToString();
                    string _BackupKeyPerson = dr["备用钥匙保管人"].ToString();
                    string _ChargePerson = dr["负责人"].ToString();
                    string _SuperChargePerson = dr["总负责人"].ToString();

                    int _OrganID = (int)Session["OrganID"];
                    string _InputBy = Session["UserID"].ToString();

                    if (string.IsNullOrEmpty(_FrameNO) || string.IsNullOrEmpty(_VehicleType) || !ValidateService.IsNumber(_VehiclePrice) || string.IsNullOrEmpty(_ObjectGroup) || string.IsNullOrEmpty(_PlanScrapDate) || string.IsNullOrEmpty(_StartUseDate))
                    {
                        row++;
                        iListErrorRowNumber.Add(row);
                        continue;
                    }


                    Vehicle i = new Vehicle();
                    i.VehicleID = _VehicleID;
                    i.FrameNO = _FrameNO;
                    i.VehicleType = GetVehicleType(_VehicleType);
                    i.VehiclePrice = Convert.ToDecimal(_VehiclePrice);
                    i.PlanScrapDate = Convert.ToDateTime(_PlanScrapDate);
                    i.StartUseDate = DateTime.Parse(_StartUseDate);
                    i.ObjectGroup = _ObjectGroup;
                    i.KeepPerson = _KeepPerson;
                    i.BackupKeyPerson = _BackupKeyPerson;
                    i.ChargePerson = _ChargePerson;
                    i.SuperChargePerson = _SuperChargePerson;

                    i.OrganID = _OrganID;
                    i.InputBy = _InputBy;

                    row++;


                    #region
                    //if ((i.PersonID.Length != 18 && i.PersonID.Length != 15) || i.DriverName.Length < 2)
                    //{ 
                    //    iListERROR.Add(i);
                    //    countERROR++;
                    //    iListErrorRowNumber.Add(n);
                    //    continue;
                    //}

                    //string s1 = "";
                    //string s18 = "0123456789X";
                    //string s15 = "0123456789";
                    //string s2 = "";
                    //if (i.PersonID.Length == 18)
                    //{
                    //    s1 = i.PersonID.Substring(6, 8);
                    //    s2 = i.PersonID;
                    //    for (int j = 0; j < 18; j++)
                    //    {
                    //        bool isfound = false;
                    //        for (int k = 0; k < s18.Length; k++)
                    //        {
                    //            if (s2.Substring(j, 1) == s18.Substring(k, 1))
                    //            {
                    //                isfound = true;
                    //                break;
                    //            }

                    //        }

                    //        if (!isfound)
                    //        {
                    //            iListERROR.Add(i);
                    //            iListErrorRowNumber.Add(n);
                    //            countERROR++;
                    //            continue;
                    //        }
                    //    }
                    //}
                    //else
                    //{
                    //    s1 = "19" + i.PersonID.Substring(6, 6);

                    //    for (int j = 0; j < 15; j++)
                    //    {
                    //        bool isfound = false;
                    //        s2 = i.PersonID;
                    //        for (int k = 0; k < s15.Length; k++)
                    //        {
                    //            if (s2.Substring(j, 1) == s15.Substring(k, 1))
                    //            {
                    //                isfound = true;
                    //                break;
                    //            }

                    //        }

                    //        if (!isfound)
                    //        {
                    //            iListERROR.Add(i);
                    //            iListErrorRowNumber.Add(n);
                    //            countERROR++;
                    //            continue;
                    //        }
                    //    }
                    //}

                    //DateTime result1 = DateTime.Now;
                    //try
                    //{
                    //    result1 = DateTime.ParseExact(s1, "yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
                    //}
                    //catch
                    //{

                    //    iListERROR.Add(i);
                    //    iListErrorRowNumber.Add(n);
                    //    countERROR++;
                    //    continue;

                    //}
                    //if (result1.AddYears(18) > DateTime.Now)
                    //{
                    //    iListERROR.Add(i);
                    //    iListErrorRowNumber.Add(n);
                    //    countERROR++;
                    //    continue;
                    //}
                    #endregion

                    try
                    {
                        int re = iBLL.Add(i);
                        if (re > 0)
                        {
                            count++;
                        }
                        else
                        {
                           iListFailRowNumber.Add(row);
                        }
                    }
                    catch
                    {
                        iListErrorRowNumber.Add(row);
                    }
                }

                string msg = "";
                if (count > 0)
                {
                    msg = msg + "成功上传数据" + count + "条!";
                    msg = msg + "\\n";
                }

                if (iListErrorRowNumber.Count > 0)
                {

                    msg = msg + "数据不全或有误共" + iListErrorRowNumber.Count + "条,请核对以下行号的数据是否有误,并进行修改!";
                    msg = msg + "\\n 有误行号:";
                    foreach (int i in iListErrorRowNumber)
                    {
                        msg += i + ",";
                    }
                    msg = msg + "\\n";
                }

                if (iListFailRowNumber.Count > 0)
                {
                    msg = msg + "车架号已存在共" + iListFailRowNumber.Count + "条!";
                    msg = msg + "\\n 已存在行号:";
                    foreach (int i in iListFailRowNumber)
                    {
                        msg += i + ",";
                    }
                    msg = msg + "\\n";
                }

                UtilityService.Alert(this.Page, msg);

            }
        }
        catch (Exception ex)
        {
            UtilityService.Alert(this.Page,"导入失败!,详细:" + ex.Message);
        }
    }


    private int GetVehicleType(string name)
    {
        if (name == "小型汽车")
            return 1;
        else if (name == "小型自动挡汽车")
            return 2;
        else if (name == "中型客车")
            return 3;
        else if (name == "大型货车")
            return 4;
        else if (name == "大型客车")
            return 5;
        else if (name == "城市公交车")
            return 6;
        else if (name == "摩托车、三轮车")
            return 7;
        else if (name == "非机动车")
            return 8;
        else
            return 1;
    }
}
